﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WpfApp.Common;

namespace WpfApp.Models
{
    public class TestModel : NotificationObject
    {
        private string testString = "WPF";

        public string TestString
        {
            get { return testString; }
            set
            {
                testString = value;
                this.RaisePropertyChanged("TestString");
            }
        }

        public void Copy(object obj)
        {
            this.TestString += " WPF";
        }

        public void CalculateKillCode(object obj)
        {
            GetRateOfKill();
            var lotteries = new List<LotteryModel>();

            var killHistory = string.Concat(AppConstants.DataFolder, "KillHistory.xml");
            var killHistory1 = string.Concat(AppConstants.DataFolder, "KillHistory1.xml");
            var killHistory2 = string.Concat(AppConstants.DataFolder, "KillHistory2.xml");

            XDocument xmlFile = XDocument.Load(string.Concat(AppConstants.DataFolder, "TDHistory.xml"));
            XDocument xmlKill = XDocument.Load(killHistory);
            XDocument xmlKill1 = XDocument.Load(killHistory1);
            XDocument xmlKill2 = XDocument.Load(killHistory2);

            var index = 0;
            lotteries = (from t in xmlFile.Descendants("Issue")
                         select new LotteryModel
                         {
                             Id = index++,
                             IssueNumber = t.Attribute("IssueNumber").Value.ToString(),
                             LotteryNumber = t.Attribute("LotteryNumber").Value.ToString(),
                             TestingNumber = t.Attribute("TestingNumber").Value.ToString(),
                             IssueDate = Convert.ToDateTime(t.Attribute("IssueDate").Value.ToString()),
                             HundredNumber = t.Attribute("LotteryNumber").Value.ToString().Split(',')[0],
                             TenNumber = t.Attribute("LotteryNumber").Value.ToString().Split(',')[1],
                             BitsNumber = t.Attribute("LotteryNumber").Value.ToString().Split(',')[2]
                         }).ToList<LotteryModel>();

            int lotteryCount = lotteries.Count();

            for (int i = lotteryCount - 1; i > 0; i--)
            {
                xmlKill.Root.AddFirst(
                                        this.GerenteKillOneCodeItem(
                                                                    lotteries[i - 1].IssueNumber,
                                                                    lotteries[i - 1].LotteryNumber,
                                                                    this.KillOneCode(lotteries[i].HundredNumber, 0),
                                                                    "",
                                                                    "",
                                                                    ""
                                        ));


                xmlKill1.Root.AddFirst(
                                        this.GerenteKillOneCodeItem(
                                                                    lotteries[i - 1].IssueNumber,
                                                                    lotteries[i - 1].LotteryNumber,
                                                                    this.KillOneCode(lotteries[i].HundredNumber, lotteries[i].TenNumber, lotteries[i].BitsNumber),
                                                                    "",
                                                                    "",
                                                                    ""
                                        ));

                xmlKill2.Root.AddFirst(
                                        this.GerenteKillOneCodeItem(
                                                                    lotteries[i - 1].IssueNumber,
                                                                    lotteries[i - 1].LotteryNumber,
                                                                    this.KillOneCode(lotteries[i].HundredNumber, 1),
                                                                    "",
                                                                    "",
                                                                    ""
                                        ));
            }

            xmlKill.Save(killHistory);
            xmlKill1.Save(killHistory1);
            xmlKill2.Save(killHistory2);
        }

        private XElement GerenteKillOneCodeItem(string issueNumber, string lotteryNumber, string killOneCode, string killOneHundred, string killOneTen, string killOneBits)
        {
            return new XElement("Item",
                                new XAttribute("IssueNumber", issueNumber),
                                new XAttribute("LotteryNumber", lotteryNumber),
                                new XAttribute("KillOneCode", killOneCode),
                                new XAttribute("KillOneHundred", killOneHundred),
                                new XAttribute("KillOneTen", killOneTen),
                                new XAttribute("KillOneBits", killOneBits));
        }

        public string KillOneCode(string hundredNumber, string tenNumber, string bitsNumber)
        {
            int[] arrayInt = new int[] { 1, 7, 5, 3, 9, 8, 6, 4, 1, 9 };

            var hundred = arrayInt[Convert.ToInt16(hundredNumber)];
            var ten = arrayInt[Convert.ToInt16(tenNumber)];
            var bits = arrayInt[Convert.ToInt16(bitsNumber)];
            var killNumber = ((hundred + ten + bits) % 10).ToString();

            return killNumber;
        }

        public string KillOneCode(string number, int index)
        {
            int[][] arrayInt = new int[][] { 
                                                new int[] { 5, 8, 6, 4, 7, 9, 1, 2, 4, 6 }, 
                                                new int[] { 0, 8, 6, 4, 7, 9, 1, 2, 4, 6 } 
                                            };

            return arrayInt[index][Convert.ToInt16(number)].ToString();
        }

        public void GetRateOfKill()
        {
            var killHistory = string.Concat(AppConstants.DataFolder, "KillHistory.xml");
            XDocument xmlKill = XDocument.Load(killHistory);
            var items = (from t in xmlKill.Descendants("Item")
                         select new KillCodeModel
                         {
                             IssueNumber = t.Attribute("IssueNumber").Value.ToString(),
                             KillOneCode = t.Attribute("KillOneCode").Value.ToString(),
                             LotteryNumber = t.Attribute("LotteryNumber").Value.ToString()
                         }).ToList();

            var currectItems = (from i in items
                                where i.LotteryNumber.IndexOf(i.KillOneCode) == -1
                                select i).ToList();

            var itemCount = items.Count;

        }

    }
}
